<?php
namespace app\controller;

use app\BaseController;


use app\model\UserMsg;
use app\Request;
use think\facade\Db;

use think\facade\View;

use app\middleware\JWTMiddleware;
use app\middleware\AuthLogin;

class Users extends BaseController
{
    // 用户详情
    public function about(Request $request)
    {
        // 获取用户id
        $user_id = AuthLogin::getAccessToken($request);

        $res = UserMsg::where(['id' => $user_id])->find();

        return json(['msg' => '获取个人信息成功!', 'data' => $res, 'code' => 200]);
    }


    // 更新用户信息
    public function update(Request $request)
    {
        // 获取用户id
        $user_id = AuthLogin::getAccessToken($request);

        $input = $request->all();

        $res = UserMsg::where(['username' => $input['username']])
            ->whereNotIn('id', $user_id)
            ->find();
        if (!$res) {
            // 如果有密码字段
            if ($input['password']) {
                $input['password'] = password_hash($input['password'], PASSWORD_DEFAULT);
            }

            $res = UserMsg::where(['id' => $user_id])->update($input);
            $data = ['msg' => '更新成功', 'code' => 200];
        } else {
            $data = ['msg' => '当前帐号已经被使用', 'code' => 403];
        }

        return json($data);
    }
}
